(function(){
	//console.log(document.getElementsByTagName("space-danmaku"));
	var acfunIndex = localStorage.getItem('acfunIndex')||0;
	//debugger;
	console.log("acfunIndex="+acfunIndex);
	if(acfunIndex==1) {
		return;
	}
	
	function  moveAB(a,b) {
		var a1 = $("<div id='a1'></div>").insertBefore(a);
		var b1 = $("<div id='b1'></div>").insertBefore(b);
		a.insertAfter(b1);
		b.insertAfter(a1);
		a1.remove();
		b1.remove();
		a1 = b1 = null;
	}	
	function Map(){
		this.container = new Object();
	}


	Map.prototype.put = function(key, value){
		this.container[key] = value;
	}


	Map.prototype.get = function(key){
		return this.container[key];
	}


	Map.prototype.keySet = function() {
		var keyset = new Array();
		var count = 0;
		for (var key in this.container) {
			// 跳过object的extend函数
			if (key == 'extend') {
			continue;
			}
			keyset[count] = key;
			count++;
		}
		return keyset;
	}


	Map.prototype.size = function() {
		var count = 0;
		for (var key in this.container) {
		// 跳过object的extend函数
		if (key == 'extend'){
		continue;
		}
		count++;
		}
		return count;
	}


	Map.prototype.remove = function(key) {
		delete this.container[key];
	}


	Map.prototype.toString = function(){
		var str = "";
		for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
		str = str + keys[i] + "=" + this.container[keys[i]] + ";\n";
		}
		return str;
	}
	var secs = new Map();
	var modleSort = [60,144,59,1,58,123,68,124,125,70,69];
	var modles    = [60,144,59,1,58,123,68,124,125,70,69];
	$('section').each(function(n,node){
		for(var xx=0;xx<modles.length;xx++) {
			if(modles[xx]==$(node).attr('data-cid')) {
				secs.put(modles[xx],node);
				break;
			}
		}
	});
	
	var sortStore = localStorage.getItem('sortStore');
	if(!!sortStore) {
		modleSort = sortStore.split(",");
	} else {
		localStorage.setItem('sortStore',modleSort);
	}
	
	for(var i=0;i<modleSort.length;i++) {
		var cc ;
		for(var j=i;j<modles.length;j++) {
			if(modleSort[i]==modles[j]) {
				cc = j;
				break;
			}
		}
		if(!!cc && cc!=i) {
			moveAB($(secs.get(modles[i])),$(secs.get(modles[cc])));
			var temp = modles[i];
			modles[i]=modles[cc];
			modles[cc] = temp;
		}
		
	}
	
	$("figure[class*='block-box']").each(function(i,a) {
		$(a).css('border-radius','0px');
	});
	
	$("a[class*='block-img']").each(function(i,a) {
		$(a).css('border-radius','0px');
	});
	
	$("a[class*='mask-video']").each(function(i,a) {
		$(a).css('border-radius','0px');
	});
	
	$("div[class*='module-video-big']").each(function(i,a) { 
		$(a).css('border-radius','0px');
	});

	$(".pic-box").each(function(i,a) { 
		$(a).css('border-radius','0px');
	});
	
	$("img").each(function(i,a) { 
		$(a).css('border-radius','0px');
	});
	
	$("div[class*='calendar-box']").each(function(i,a) { 
		$(a).css('border-radius','0px');
	});
	$('#toolbar').before($('<div class="toolbar" id="div2120" style="opacity: 0.0;border-radius: 4px;transition: .3s;background: rgba(255,255,255,0.8);width:0px;height:0px;bottom:23px;display: none;z-index:8"></div>'));

	var bodyWith = $('#div2120').parent().css('width').replace('px','');
	bodyWith = bodyWith*0.5-143;
	$('#div2120').css('left',bodyWith+'px');
	console.log('window.onresize=');
	window.onresize  = function() {
		var bodyWith = $('#div2120').parent().css('width').replace('px','');
		bodyWith = bodyWith*0.5-143;
		$('#div2120').css('left',bodyWith+'px');
	}
	
	var clicked = 'Yeah.';//"Nope.";
	var mausx = "0";
	var mausy = "0";
	var winx = "0";
	var winy = "0";
	var oldTop = 0;
	var oldx=0;
	var oldy=0;
	var isEdit = 0;
	var difx = mausx - winx;
	var dify = mausy - winy;
	var moveNodeId ;
	var m = new Map();
	var isClick = 0;
	function doChanger(i,id,node) {
		
		$(window).scroll(function(){
			var winPos = $(window).scrollTop();
			var h = 100+winPos-$(node).offset().top;
			if(h>-100&&h<150 && isClick==0) {
				//$(id).trigger("click");
				$("div[id^='select-node']").css("background-color","rgba(0,0,0,0.8)");
				$("div[id^='select-node']").css("color","#fff");
				$("div[id^='select-node']").attr("isSelect","0");
				
				$(id).css("background-color","#00A1D6");
				$(id).attr("isSelect","1");
				$(id).css("color","#fff");
			}
			
		});
		
		$(id).Mnode = node;
		$(id).hover(function(){
			if($(this).attr("isSelect")=="1")
			return;
			$(this).css("background-color","#00A1D6");
		
			$(this).css("color","#fff");
		},function(){
			if($(this).attr("isSelect")=="1")
			return;
			$(this).css("background-color","rgba(0,0,0,0.8)");
			
			$(this).css("color","#fff");
		});
		
		var thisId = id;
		$(id).mousemove(function (event){
			//console.log(thisId);
		});
		$(id).click(function(){
			console.log('click');
			isClick=1;
			if(isEdit==1) {
				$(moveNodeId).css("cursor","move");
			}
			$("div[id^='select-node']").css("background-color","rgba(0,0,0,0.8)");
			$("div[id^='select-node']").css("color","#fff");
			$("div[id^='select-node']").attr("isSelect","0");
			//var top = $(this).css("top");
			//$('#select-node-a').css('display','block');
			//$('#select-node-a').css('top',top);
			//$('#select-node-a').html($(this).html());
			//$('#select-node-a').css('display','none');
			$(this).css("background-color","#00A1D6");
			$(this).attr("isSelect","1");
		    $(this).css("color","#fff");
			$("html,body").stop(true);
			$("html,body").animate({scrollTop:$(node).offset().top-100},300,null,function(){
				isClick=0;
			});
			
			return false;
		});
		
		$(id).mousedown(function (event) {
			console.log('mousedown');
			console.log('oldx='+oldx+"oldy="+oldy);
			if(isEdit==0) {
				oldx=0;
				oldy=0;
				return;
			}
			$("div[id^='select-node']").css('z-index','11002');
			$(id).css('z-index','11003');
            clicked = "Yeah.";
			moveNodeId = id;
			
			console.log("按下"+id);
			if(oldy==0) {
				oldTop = $(moveNodeId).css('top');
				oldx = $(moveNodeId).offset().left;
				oldy = $(moveNodeId).offset().top;
			}
        });
		
		$(id).mouseup(function (event) {
			console.log('mouseup');
			console.log('oldx='+oldx+"oldy="+oldy);
			if(isEdit==0) {
				oldx=0;
				oldy=0;
				mausx = 0;
				mausy = 0;
				winx = 0;
				winy = 0;
				difx = mausx - winx;
				dify = mausy - winy;
				return;
			}
			$("div[id^='select-node']").css('z-index','11002');
            clicked = "Nope.";
			if(Math.abs($(moveNodeId).offset().left-oldx)>50) {
				$(moveNodeId).offset({ top: oldy, left: oldx });
				oldx=0;
				oldy=0;
				mausx = 0;
				mausy = 0;
				winx = 0;
				winy = 0;
				difx = mausx - winx;
				dify = mausy - winy;
			}
			
			if(Math.abs($(moveNodeId).offset().left-oldx)<=50 && Math.abs($(moveNodeId).offset().top-oldy)>18) {
				var i = Math.round(Math.abs($(moveNodeId).offset().top-oldy)/32);
				console.log("i=="+i);
				var tnodt ;
				var tmp = $(id);
				for(var x=0;x<i;x++) {
					if(tmp) {
						if($(moveNodeId).offset().top-oldy>0) {
							tnodt = tmp.next();
						} else {
							tnodt = tmp.prev();
						}
						if(tnodt)
							tmp = tnodt;
						else
							tnodt = tmp;
					}
				}
				//var nx = tnodt.offset().left;
				//var ny = tnodt.offset().top;
				var newTop = $(tnodt).css('top');
				//tnodt.offset({ top: oldy, left: oldx });
				//$(id).offset({ top: ny, left: nx });
				$(tnodt).css('top',oldTop);
				$(id).css('top',newTop);
				var n = $(m.get(id)).parent(), p = $(m.get("#"+$(tnodt).attr("id"))).parent();
				if(n[0].nodeName=="DIV") {
					n = $(n).parent();
				} else {
					n = n;
				}
				if(p[0].nodeName=="DIV") {
					p = $(p).parent();
					
				} else {
					p = p;
				}
				//border-radius
				var rad = $(tnodt).css('border-radius');
				$(tnodt).css('border-radius',$(moveNodeId).css('border-radius'));
				$(moveNodeId).css('border-radius',rad);
				$(tnodt).css('left','0');
				$(moveNodeId).css('left','0');
				moveAB($(tnodt),$(moveNodeId));
				
				
				$(id).css('position', '');
				moveAB($(n),$(p));
				var toi,toj;
				for(var xx=0;xx<modles.length;xx++) {
					if(modleSort[xx]==$(n).attr('data-cid')) {
						toi = xx;
						break;
					}
				}
				if($(moveNodeId).offset().top-oldy>0) {
					toj = toi+i;
				}else {
					toj = toi-i;
				}
				
				var tmp = modleSort[toi];
				modleSort[toi] = modleSort[toj];
				modleSort[toj] = tmp;
				localStorage.setItem('sortStore',modleSort);
				oldx=0;
				oldy=0;
				$("div[id^='select-node'").css("cursor","pointer");
				$(moveNodeId).css("cursor","move");
			} else {
				$(moveNodeId).offset({ top: oldy, left: oldx });
				oldx=0;
				oldy=0;
				mausx = 0;
				mausy = 0;
				winx = 0;
				winy = 0;
				difx = mausx - winx;
				dify = mausy - winy;
			}
        });
		
	}
	
	
		$("#toolbar").mousemove(function (event) {
			console.log("mousemove");
			if(isEdit==0 ||$(moveNodeId).attr("isSelect")!="1") {
				return;
			}
            mausx = event.pageX;
            mausy = event.pageY;
			if(!$(moveNodeId)||!$(moveNodeId).offset()) {
				return;
			}
			
            winx = $(moveNodeId).offset().left;
            winy = $(moveNodeId).offset().top;
            if (clicked == "Nope.") {
                difx = mausx - winx;
                dify = mausy - winy;
            }
            var newx = event.pageX - difx - $(moveNodeId).css("marginLeft").replace('px', '');
            var newy = event.pageY - dify - $(moveNodeId).css("marginTop").replace('px', '');
			if (clicked == "Nope.") {
				return;
			}
            $(moveNodeId).offset({ top: newy, left: newx });

        });
	
	var cdd =0;
	$("header[class*='area-header']").each(function(i,a) { 
		var ma = $(a).find('b a');
		if(ma.length>0) {
			//top:'+(i-2)*32+'px;
			console.log(ma[0].innerText);
			if(cdd==0) { 
				$('#toolbar').prepend('<div id="select-node-'+cdd+'" class="tool-item tool-to-comm" style="transition:.1s linear;transition-property:background-color,color;line-height: 32px;border-radius:3px 3px 0 0;display:block;margin:0 0 0;font-size: 12px;height: 32px;">'+ma[0].innerText+'</div>');
			} else {
				$('#select-node-'+(cdd-1)).after('<div id="select-node-'+cdd+'" class="transition:.1s linear;transition-property:background-color,color;tool-item tool-to-comm" style="line-height: 32px;border-radius: 0px;display:block;margin:0 0 0;font-size: 12px;height: 32px;">'+ma[0].innerText+'</div>');
			}
			//$('#mytoolbar').append($('<div id="select-node-'+i+'" class="tool-to-top" style="top:'+(i-2)*32+'px;transition:.1s linear;transition-property:background-color,color;height:32px;font-size:12px;line-height:32px;border-radius: 0px;">'+ma[0].innerText+'</div>'));
			doChanger(i,'#select-node-'+cdd,a);
			m.put('#select-node-'+cdd,a);
			cdd++;
		}
	});
	console.log('#select-node-'+(cdd-1));
	$('#select-node-'+(cdd-1)).after('<div id="sort-select-node" class="tool-item tool-to-comm" style="margin:0 0 0;display:block;transition:.1s linear;transition-property:background-color,color;height:64px;font-size:12px;line-height:95px;border-radius: 0 0 3px 3px">排序</div>');
	$("#sort-select-node").css('background-image','url("")');
	$("#div2120").css('background-image','url("")');
	$('#toolbar').css('bottom','400px');
	
	$("#div2120").css('background-repeat','no-repeat');
	$("#div2120").css('background-position','23px 40px');
	$("#sort-select-node").css('background-repeat','no-repeat');
	$("#sort-select-node").css('background-size','26px 26px');
	$("#sort-select-node").css('background-position','12px 10px');
	$("#sort-select-node").hover(function(){
			$(this).css("background-color","#00A1D6");
		
			$(this).css("color","#fff");
		},function(){
			 
			$(this).css("background-color","rgba(0,0,0,0.8)");
			
			$(this).css("color","#fff");
		});
		
		$("#sort-select-node").click(function(){
			
			if(isEdit==0) {
				oldx=0;
				oldy=0;
				isEdit=1;
				
				$('body').append($('<div id="div0018" style="display:none;top:0px;letf:0px;width:100%;height:200%;position: fixed;background-color: #000;opacity: .5!important;    z-index: 10005;"></div>'));
				$("#div0018").css('display','block');
				
				$("#div2120").css('display','block');
				$("#div2120").css('z-index','11000');
				$("#div2120").animate({width:"215px",height:"520px",opacity: 1},100);
				$("#toolbar").css('z-index','11001');
				
				
				$('#div0018').click(function(){
					$("#div0018").remove();
					$("#div2120").animate({width:"0px",height:"0px",opacity: 0.0},100,0,function(){
						$("#div2120").css('z-index','1');
						$("#toolbar").css('z-index','1');
					});
					isEdit=0;
						$("#toolbar").css("cursor","pointer");
						$("div[id^='select-node'").css("cursor","pointer");						
				});
			} else {
				isEdit=0;
				$("#div0018").remove();
				$("#div2120").animate({width:"0px",height:"0px",opacity: 0.0},100,0,function(){
						$("#div2120").css('z-index','1');
						$("#toolbar").css('z-index','1');
					});
				$("div[id^='select-node'").css("cursor","pointer");				
			}
			
		});
		
		
	$('#slider-big').css('border-radius','0px');
	$('#calendar-anchor').remove();
	$("#main li").each(function(i,a) { 
		 $(a).css('border-radius','0px');
	});

	
})();



(function(){
        chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
			console.log(request.type);
			if(request.type=="acwin") {
				localStorage.setItem('sortStore','');
				if("http://www.acfun.tv/"==location.href || "http://hengyang.acfun.tv/"==location.href ) {
					history.go(0) ;
				}
			} else if(request.type=="blacklist") {
				console.log('12');
				
			}
			localStorage.setItem(request.type,request.value);
        });
})();
  
